
import store from '../store'
var stringutils = function () {
    var self = {};
    self.isIosOrAndroid = function () {
        var u = navigator.userAgent;
        var isAndroid = u.indexOf("Android") > -1 || u.indexOf("Linux") > -1; //android终端或者uc浏览器 
        var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端 
        if (isAndroid) {
            return 'android';
        }
        if (isIOS) {
            return 'ios';
        }
        return "";
    }
    
    //数组大小排序
    self.sortArray = function (data) {
        return data.sort(function(a,b){return a-b});
    }
    //数组去重或者new Set使用
    self.duplicateArr = function (data) {
        var arr = [];
        for (var i = 0,lens=data.length; i < lens; i++) {
            if (arr.indexOf(data[i]) == -1) {
                arr.push(data[i]);
            }
        }
        return arr;
        // return Array.from(new Set(data));
    }
    //表格排序
    self.sortBy = function (attr, rev) {
        return function (a, b) {
            a = Number(a[attr]); 
            b = Number(b[attr]); 
            if (a < b) {
                return rev * -1
            }
            if (a > b) {
                return rev * 1
            }
            return 0
        }
    }
    //深拷贝
    self.deepClone = function (obj) {
        var objClone = Array.isArray(obj) ? [] : {};
        if (obj && typeof obj === "object") {
            for (var key in obj) {
                if (obj.hasOwnProperty(key)) {
                    //判断ojb子元素是否为对象，如果是，递归复制
                    if (obj[key] && typeof obj[key] === "object") {
                        objClone[key] = this.deepClone(obj[key]);
                    } else {
                        //如果不是，简单复制
                        objClone[key] = obj[key];
                    }
                }
            }
        }
        return objClone;
    }
    //下载base64图片
    self.downloadFile = function (imgFile) {
        const imgUrl = imgFile;
        if (window.navigator.msSaveOrOpenBlob) {
            var bstr = atob(imgUrl.split(',')[1]);
            var n = bstr.length;
            var u8arr = new Uint8Array(n);
            while (n--) {
                u8arr[n] = bstr.charCodeAt(n);
            }
            var blob = new Blob([u8arr]);
            window.navigator.msSaveOrOpenBlob(blob, 'qrcode-download' + '.' + 'png');
        } else {
            const a = document.createElement('a');
            a.href = imgUrl;
            a.setAttribute('download', 'qrcode-download');
            a.click();
            
        }
    
    }
    self.price = function (value) {
        if (!value) {
            return '0.00'
        }
        if (!value||Number(value<0)) {
            return '0.00'
        }
        value = Number(value).toFixed(2); //保留两位小数 四舍五入
        return value;
    }
    self.clearData = function () {
        store.dispatch('setToken', '');
        store.dispatch('setUserInfo', null);
        store.dispatch('setOriClassList', []);
        store.dispatch('setClassList', []);
        store.dispatch('setSearchHistory', []);
    }
    return self;
}();


export {
    stringutils
}